package com.system.uitls;

import java.util.ArrayList;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;

public class PinyingUtil {
	public String converterToFirstSpell(String chines) {
		StringBuffer pinyinName = new StringBuffer();
		char[] nameChar = chines.toCharArray();
		HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
		defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);
		defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
		for (int i = 0; i < nameChar.length; i++) {
			if (nameChar[i] > '') {
				try {
					String[] strs = PinyinHelper.toHanyuPinyinStringArray(
							nameChar[i], defaultFormat);
					if (strs != null) {
						for (int j = 0; j < strs.length; j++) {
							pinyinName.append(strs[j].charAt(0));
							if (j != strs.length - 1) {
								pinyinName.append(",");
							}

						}
					}
				} catch (BadHanyuPinyinOutputFormatCombination e) {
					e.printStackTrace();
				}
			} else {
				pinyinName.append(nameChar[i]);
			}
			pinyinName.append(" ");
		}

		return parseTheChineseByObject(discountTheChinese(pinyinName.toString()));
	}

	public String converterToSpell(String chines) {
		StringBuffer pinyinName = new StringBuffer();
		char[] nameChar = chines.toCharArray();
		HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
		defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);
		defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
		for (int i = 0; i < nameChar.length; i++) {
			if (nameChar[i] > '') {
				try {
					String[] strs = PinyinHelper.toHanyuPinyinStringArray(
							nameChar[i], defaultFormat);
					if (strs != null) {
						for (int j = 0; j < strs.length; j++) {
							pinyinName.append(strs[j]);
							if (j != strs.length - 1)
								pinyinName.append(",");
						}
					}
				} catch (BadHanyuPinyinOutputFormatCombination e) {
					e.printStackTrace();
				}
			} else {
				pinyinName.append(nameChar[i]);
			}
			pinyinName.append(" ");
		}

		return parseTheChineseByObject(discountTheChinese(pinyinName.toString()));
	}

	private List<Map<String, Integer>> discountTheChinese(String theStr) {
		List mapList = new ArrayList();

		Map onlyOne = null;
		String[] firsts = theStr.split(" ");

		for (String str : firsts) {
			onlyOne = new Hashtable();
			String[] china = str.split(",");

			for (String s : china) {
				Integer count = (Integer) onlyOne.get(s);
				if (count == null) {
					onlyOne.put(s, new Integer(1));
				} else {
					onlyOne.remove(s);
					count = Integer.valueOf(count.intValue() + 1);
					onlyOne.put(s, count);
				}
			}
			mapList.add(onlyOne);
		}
		return mapList;
	}

	private String parseTheChineseByObject(List<Map<String, Integer>> list) {
		Map first = null;
		String s;
		for (int i = 0; i < list.size(); i++) {
			Map temp = new Hashtable();

			if (first != null) {
				for (Object o : first.keySet()) {
					String ss = (String) o;
					for (Object oo : ((Map) list.get(i)).keySet()) {
						String s1 = (String) oo;
						String str = ss + s1;
						temp.put(str, Integer.valueOf(1));
					}
				}

				if ((temp != null) && (temp.size() > 0))
					first.clear();
			} else {
				Iterator<String> iterator = ((Map) list.get(i)).keySet().iterator(); 
				while(iterator.hasNext()){
					s = iterator.next();
					String str = s;
					temp.put(str, Integer.valueOf(1));
				}
				/*for (Iterator<String> iterator = ((Map) list.get(i)).keySet()
						.iterator(); iterator.hasNext();) {
					s = iterator.next();
					String str = s;
					temp.put(str, Integer.valueOf(1));
				}*/
			}

			if ((temp != null) && (temp.size() > 0)) {
				first = temp;
			}
		}
		String returnStr = "";
		if (first != null) {
			for (Object o : first.keySet()) {
				String str = (String) o;
				returnStr = returnStr + str + ",";
			}
		}
		if (returnStr.length() > 0) {
			returnStr = returnStr.substring(0, returnStr.length() - 1);
		}
		return returnStr;
	}

	public static void main(String[] args) {
		String str = "长沙";
		System.out.println(new PinyingUtil().converterToFirstSpell(str));
	}
}